package com.jiazhong.example.project.shoppingcart.controller;

import com.alibaba.fastjson.JSONArray;
import com.jiazhong.example.project.shoppingcart.bean.Carts;
import com.jiazhong.example.project.shoppingcart.bean.Users;
import com.jiazhong.example.project.shoppingcart.service.CartsService;
import com.jiazhong.example.project.shoppingcart.service.impl.CartsServiceImpl;
import com.jiazhong.example.project.shoppingcart.util.JsonResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/carts")
public class CartsController extends BaseController {

    private CartsService cartsService = new CartsServiceImpl();

    public void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int bookId = Integer.parseInt(request.getParameter("book_id"));
        HttpSession session = request.getSession();
        Users user = (Users) session.getAttribute("user");
        int userId = user.getId();

        Carts carts = new Carts();
        carts.setBookId(bookId);
        carts.setUserId(userId);

        JsonResult result = cartsService.saveOrUpdate(carts);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(JSONArray.toJSONString(result));
    }

    public void find_user_id(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Users user = (Users) session.getAttribute("user");
        int userId = user.getId();
        JsonResult result = cartsService.findByUserId(userId);
        System.out.println(result);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(JSONArray.toJSONString(result));
    }

    public void change_cart_number(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int number = Integer.parseInt(request.getParameter("number"));
        int bookId = Integer.parseInt(request.getParameter("book_id"));
        HttpSession session = request.getSession();
        Users user = (Users) session.getAttribute("user");
        int userId = user.getId();
        JsonResult result = cartsService.changeCartNumber(userId, bookId, number);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(JSONArray.toJSONString(result));
    }

    public void delete_by_id(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        JsonResult result = cartsService.deleteById(id);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(JSONArray.toJSONString(result));
    }

    public void delete_by_batch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String ids = request.getParameter("ids");
        JsonResult result = cartsService.deleteByBatch(ids);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(JSONArray.toJSONString(result));

    }

    public void delete_all(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        JsonResult result = cartsService.deleteAll(request.getSession());
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(JSONArray.toJSONString(result));
    }

}
